package nc.bs.pub.action;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import nc.bs.common.projectFAUtils;
import nc.bs.common.projectFAUtils;
import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.NCLocator;
import nc.bs.ic.pub.env.ICBSContext;
import nc.bs.pub.compiler.AbstractCompiler2;
import nc.itf.ic.m45.self.IPurchaseInMaintain;
import nc.itf.uif.pub.IUifService;
import nc.jdbc.framework.processor.ColumnListProcessor;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.vo.ic.m45.entity.BillCPVO;
import nc.vo.ic.m45.entity.PurchaseInBodyVO;
import nc.vo.ic.m45.entity.PurchaseInHeadVO;
import nc.vo.ic.m45.entity.PurchaseInVO;
import nc.vo.ic.pub.pf.ICPFParameter;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.pub.BusinessException;
import nc.vo.pub.compiler.PfParameterVO;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.trade.billsource.LightBillVO;
import nc.vo.trade.billsource.LinkQueryType;

public class N_45_DELETE extends AbstractCompiler2 {
	private Hashtable m_methodReturnHas = new Hashtable();
	private Hashtable m_keyHas = null;
	private final  String message="N";
	private String billNO = "";
	private String billFinderClassname = "nc.impl.pubapp.linkquery.BillTypeSetBillFinder";
	private static IUifService service = (IUifService)NCLocator.getInstance().lookup(IUifService.class);
	public Object runComClass(PfParameterVO vo) throws BusinessException {
		try {
			
			
			super.m_tmpVo = vo;
			ICBSContext context = new ICBSContext();
			context.setICPFParameter((ICPFParameter) this.getUserObj());
			PurchaseInVO[] inVOs = (PurchaseInVO[]) ((PurchaseInVO[]) this.getVos());
			
			//lhw 231207  begin
			new projectFAUtils().projectDataApprove("ic_purchasein_h",inVOs, "delete");
			//end
			
			 for(PurchaseInVO purchaseInVO:inVOs) {
				 PurchaseInVO[] inVOsC = (PurchaseInVO[]) ((PurchaseInVO[]) this.getVos());
				 PurchaseInHeadVO pcvo= (PurchaseInHeadVO) inVOsC[0].getParentVO();            					
					PurchaseInBodyVO[]	pcbvos=(PurchaseInBodyVO[]) purchaseInVO.getAllChildrenVO();          					
					for(PurchaseInBodyVO   pcbVo:pcbvos) {			
						List<BillCPVO>   bills=getNobyId(pcvo.getCgeneralhid());
						
						String updateCode=null;
						for(BillCPVO bvo:bills) {
							LightBillVO lvo=bvo.getLightBillVO();
							if(lvo.getType().equals("422X")) {
								updateCode =lvo.getCode();
							}
							
							
						}
					//修改sql
//				String updateCode=(String) new BaseDAO().executeQuery(sql,new ColumnProcessor());
						
						
				if(updateCode!=null) {
					String sqlNumber="select nassistnum from  ic_purchasein_b where dr=0 and cgeneralbid='"+pcbVo.getCgeneralbid()+"'";
					BigDecimal noNumber=(BigDecimal) new BaseDAO().executeQuery(sqlNumber, new ColumnProcessor());
					BigDecimal big= BigDecimal.ZERO;
					if(noNumber!=null) {
						 big=noNumber;
					}
			
					String sql="SELECT pob.vsourcerowno FROM po_order  po left join po_order_b pob  on po.pk_order =pob.pk_order WHERE po.vbillcode ='"+pcbVo.getVfirstbillcode()+"' AND pob.crowno='"+pcbVo.getVfirstrowno()+"' and pob.dr=0";
					
					String no=(String) new BaseDAO().executeQuery(sql, new ColumnProcessor());
					
					if(no!=null) {
						if(!no.equals("~")) {
					
							List<BillCPVO>   billss=getNobyId(pcvo.getCgeneralhid());
							
							String updateCodez =null;
							for(BillCPVO bvo:billss) {
								LightBillVO lvo=bvo.getLightBillVO();
								if(lvo.getType().equals("422X")) {
									updateCodez =lvo.getCode();
								}
							}
					        String sqlnumber ="select pk_storereq_b  from po_storereq_b psb LEFT JOIN  po_storereq ps  ON  ps.pk_storereq = psb.pk_storereq  where  psb.dr=0 and ps.vbillcode='"+updateCodez+"' and psb.crowno='"+ no +"'";
								
					        String pkNumber=(String) new BaseDAO().executeQuery(sqlnumber, new ColumnProcessor());
							String sqla ="select vbdef1  from po_storereq_b	where  dr=0 and pk_storereq_b='"+pkNumber+"'";
							
						    String numberc=(String) new BaseDAO().executeQuery(sqla, new ColumnProcessor());
						    BigDecimal bigc= BigDecimal.ZERO;
							if(numberc!=null) {
								if(!numberc.equals("~")) {
									bigc=new BigDecimal(numberc);
								}
							}
						    
							
							
							BigDecimal bd=bigc.subtract(big);
							
							if(bd.compareTo(BigDecimal.ZERO)<0) {
								throw new BusinessException("数据存在问题，请检查");
							}
							//修改调整
							String  updateSql="update po_storereq_b set vbdef1 ='"+bd.toPlainString()+"' where pk_storereq_b ='"+pkNumber+"' ";	
							new BaseDAO().executeUpdate(updateSql);
							
							String sqlcheck ="select pk_storereq   from po_storereq_b	where  dr=0 and pk_storereq_b='"+pkNumber+"'";
							
							String numbersc=(String) new BaseDAO().executeQuery(sqlcheck, new ColumnProcessor());
							
							String sqlchecks ="select  vbdef1  from po_storereq_b	where  dr=0 and pk_storereq='"+numbersc+"'";
						    List<String> numberList=(List<String>) new BaseDAO().executeQuery(sqlchecks, new ColumnListProcessor());
						    int flag=0;
							for(String number:numberList) {
								if(null!=number&&!number.equals("0.00000000")&&!number.equals("~")) {
								  	flag=1;
								}
							}
							
								if(flag==0) {
									
									String  updateSqls="update po_storereq set vdef1 ='"+message+"' where vbillcode ='"+updateCodez+"' ";	
									//修改sql
									new BaseDAO().executeUpdate(updateSqls);	
									
									
								}
								
								
					
						}
				
					}
				}
			}
			
			
			
		 }
			 ((IPurchaseInMaintain) NCLocator.getInstance().lookup(IPurchaseInMaintain.class)).delete(inVOs);
			return null;
		} catch (Exception var4) {
			ExceptionUtils.marsh(var4);
			return null;
		}
	}

	public String getCodeRemark() {
		return NCLangRes4VoTransl.getNCLangRes().getStrByID("4008002_0", "04008002-0076");
	}

	private void setParameter(String key, Object val) {
		if (this.m_keyHas == null) {
			this.m_keyHas = new Hashtable();
		}

		if (val != null) {
			this.m_keyHas.put(key, val);
		}

	}
	
	public List<BillCPVO> getNobyId(String id){
		LinkQueryType queryType = LinkQueryType.OnlyQueryBill;
		LightBillVO vo = null;

		try {
			vo = service.queryBillGraph(billFinderClassname, id,"45", queryType);
			if (vo != null && StringUtils.isEmpty(vo.getCode())) {
				vo.setCode(billNO);
			}
		} catch (Exception var9) {
			String message = NCLangRes4VoTransl.getNCLangRes().getStrByID("0020nccloud_0", "0ncc0020001-0277");
			Exception ex = new RuntimeException(var9);
			Throwable cause = ExceptionUtils.unmarsh(ex);
			if (var9 != null && cause.getMessage() != null) {
				message = cause.getMessage();
			}

			ExceptionUtils.wrappBusinessException(message);
		}
		
		List<BillCPVO> list = this.dealVO(vo);
		return list;
		}
		
		
		
		
		
		
		
		public List<BillCPVO> dealVO(LightBillVO vo) {
			if (vo == null) {
				return null;
			} else {
				Map<String, BillCPVO> setAll = new LinkedHashMap();
				this.getSource(vo, setAll);
				List<String> sourceList = ((BillCPVO) setAll.get(vo.getID())).getSourceList();
				this.getForward(vo, setAll);
				((BillCPVO) setAll.get(vo.getID())).setIsForward("0");
				((BillCPVO) setAll.get(vo.getID())).setSourceList(sourceList);
				List<BillCPVO> list = new ArrayList(setAll.values());
				return list;
			}
		}

		public void getSource(LightBillVO vo, Map<String, BillCPVO> setAll) {
			if (vo != null) {
				BillCPVO flow = new BillCPVO();
				flow.setLightBillVO(vo);
				flow.setIsForward("-1");
				List<String> sourceList = flow.getSourceList();
				if (vo.getSourceBillVOs() == null) {
					if (setAll.get(vo.getID()) == null) {
						setAll.put(vo.getID(), flow);
					}

				} else {
					LightBillVO[] var5 = vo.getSourceBillVOs();
					int var6 = var5.length;

					for (int var7 = 0; var7 < var6; ++var7) {
						LightBillVO sourVO = var5[var7];
						sourceList.add(sourVO.getID());
						this.getSource(sourVO, setAll);
					}

					vo.setSourceMap(new HashMap());
					flow.setSourceList(sourceList);
					setAll.put(vo.getID(), flow);
				}
			}
		}

		public void getForward(LightBillVO vo, Map<String, BillCPVO> setAll) {
			if (vo != null) {
				BillCPVO flow = new BillCPVO();
				flow.setLightBillVO(vo);
				flow.setIsForward("1");
				List<String> forwardList = flow.getForwardList();
				if (vo.getForwardBillVOs() == null) {
					if (setAll.get(vo.getID()) == null) {
						setAll.put(vo.getID(), flow);
					}

				} else {
					LightBillVO[] var5 = vo.getForwardBillVOs();
					int var6 = var5.length;

					for (int var7 = 0; var7 < var6; ++var7) {
						LightBillVO sourVO = var5[var7];
						forwardList.add(sourVO.getID());
						this.getForward(sourVO, setAll);
					}

					vo.setForwardMap(new HashMap());
					flow.setForwardList(forwardList);
					setAll.put(vo.getID(), flow);
				}
			}
		}
}
		
